//
//  CarProductionOrderController.m
//  XTCloud
//
//  Created by QA on 2017/3/2.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CarProductionOrderController.h"
#import "CarProductionOrderCell.h"

#import "MJRefresh.h"
#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"
#import "WebServiceCommonRequest.h"

#import "UIColorDispose.h"

@interface CarProductionOrderController ()

@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *resultData;

@property (strong, nonatomic) NSString *lineID;

@end

@implementation CarProductionOrderController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle:@"生产排单"];
    }
    return self;
}

- (instancetype)initWithLineID:(NSString*)lineID
{
    self = [super init];
    if (self) {
        self.lineID = lineID;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resultData = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    [self.tableView registerClass:[CarProductionOrderCell class] forCellReuseIdentifier:@"CarProductionOrderCell1"];
    [self setupRefresh];
}

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    [self getNetWorkData];
}

#pragma mark 网络获取数据

- (void)getNetWorkData
{
    //站点ID
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    
    //@车辆状态（全部、出厂、待命、异常）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"CLDT_HOME_CLXX_PCD"];
    [content setStationID:[model.stationId integerValue]];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content addParameterKey:@"生产线ID" value:self.lineID];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [self.tableView headerEndRefreshing];
        //添加数据，刷新页面
        if(result.resultArray && result.resultArray.count>0){
            [self.resultData removeAllObjects];
            NSArray *clxxArray = result.resultArray[0];
            if (clxxArray && clxxArray.count>0) {
                [self.resultData addObjectsFromArray:clxxArray];
                [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            } else {
                [self.tableView setTableViewNotDataDrawText:@"暂无排产单" isShow:YES];
            }
        } else {
            [self.tableView setTableViewNotDataDrawText:@"暂无排产单" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"无法连接网络"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarProductionOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarProductionOrderCell1" forIndexPath:indexPath];
    
    NSDictionary *itemInfo = [self.resultData objectAtIndex:indexPath.row];
    cell.ztLable.text = [itemInfo objectFormatStringForKey:@"状态" defaults:@"未知"];
    cell.sjLable.text = [itemInfo objectFormatStringForKey:@"司机" defaults:@"未收录"];
    cell.chLable.text = [itemInfo objectFormatStringForKey:@"车号" defaults:@"未知"];
    cell.gcmcLable.text = [itemInfo objectFormatStringForKey:@"工程名称" defaults:@"未知"];
    cell.sgbwLable.text = [itemInfo objectFormatStringForKey:@"施工部位" defaults:@"未知"];
    cell.qddjLable.text = [itemInfo objectFormatStringForKey:@"强度等级" defaults:@"未知"];
    cell.scxLable.text = [itemInfo objectFormatStringForKey:@"生产线" defaults:@"--"];
    
    NSInteger lineNumber = [itemInfo objectFormatIntegerForKey:@"生产线"];
    if(lineNumber == 0) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#42A5F5"];
    if(lineNumber == 1) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#42A5F5"];
    if(lineNumber == 2) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#FFA726"];
    if(lineNumber == 3) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#66BB6A"];
    if(lineNumber == 4) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#26A69A"];
    if(lineNumber == 5) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#5C6BC0"];
    if(lineNumber == 6) cell.scxLable.backgroundColor = [UIColorDispose colorWithHexString:@"#8D6E63"];
    
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

